import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.GridLayout;
import javax.swing.border.*;


/**
 * EBook Organizer
 * 4/30/2010
 * <P>
 * The main window to use with the <code>EBookOrganizer</code>.
 * </P>
 * @version 0.1
 */
public class OrganizerView extends JFrame implements ActionListener{
	/**
	 * The SerialVersion UID
	 */
	private static final long serialVersionUID = -5309223964978181561L;
	
	/**
	 * The <code>OrganizerModel</code> to use.
	 */
	private OrganizerModel model;
	
	/**
	 * Components
	 */
	private JMenuBar jmbMenuBar = new JMenuBar();
	private JMenu jmFile = new JMenu("File");
	
	private JMenuItem jmiReLoad = new JMenuItem("ReLoad");
	private JMenuItem jmiLoad = new JMenuItem("Load");
	private JMenuItem jmiSave = new JMenuItem("Save");
	private JMenuItem jmiSaveAs = new JMenuItem("SaveAs");
	private JMenuItem jmiClose = new JMenuItem("Close");
	
	private JMenu jmAdd = new JMenu("Add");
	private JMenuItem jmiAddBook = new JMenuItem("EBook");
	private JMenuItem jmiAddFolder = new JMenuItem("Folder");
	private JMenuItem jmiAddFolderRec = new JMenuItem("Folder recursivly");
	
	private JMenu jmEdit = new JMenu("Edit");
	private JMenuItem jmiEditRename = new JMenuItem("Rename");
	
	private JMenu jmOptions = new JMenu("Options");
	private JMenuItem jmiSettings = new JMenuItem("Settings");
	private JMenuItem jmiAbout = new JMenuItem("About");
	
	
	private DefaultListModel folderListModel = new DefaultListModel();
	private JList folderList = new JList( folderListModel );
	private JButton jbtClearFolders = new JButton("Remove All");
	private JButton jbtRemoveFolder = new JButton("Remove Selected");
	
	private DefaultListModel bookListModel = new DefaultListModel();
	private JList bookList = new JList( bookListModel );
	private JButton jbtOpen = new JButton("Open");
	
	private FavoritesTreePanel favoritesTree = new FavoritesTreePanel();
	private JButton jbtAddFavoriteBook = new JButton("AddBook");
	private JButton jbtAddFavoriteFolder = new JButton("AddFolder");
	private JButton jbtRemoveFavorite = new JButton("Remove");
	private JButton jbtClearFavorites = new JButton("Clear All");
	private JButton jbtOpenFavorite = new JButton("Open");
	
	private AttributesPanel attributesPanel = new AttributesPanel();
	
	
	public OrganizerView () {
		this.setTitle("Ebook Organizer");
		
		jmFile.add( jmiReLoad );
		jmFile.add(jmiLoad);
		jmFile.add(jmiSave);
		jmFile.add(jmiSaveAs);
		jmFile.add(jmiClose);
		jmbMenuBar.add(jmFile);
		
		jmiAddBook.setEnabled( false );			/** Remove when added */
		jmAdd.add(jmiAddBook);
		jmAdd.add(jmiAddFolder);
		jmiAddFolderRec.setEnabled( false );	/** Remove when added */
		jmAdd.add(jmiAddFolderRec);
		jmbMenuBar.add(jmAdd);
		
		jmiEditRename.setEnabled( false );		/** Remove when added */
		jmEdit.add( jmiEditRename );
		jmbMenuBar.add( jmEdit );
		
		jmOptions.add(jmiSettings);
		jmOptions.add(jmiAbout);
		jmbMenuBar.add(jmOptions);
		
		this.setJMenuBar( jmbMenuBar );
		
		this.setLayout( new GridLayout(2,2) );
		
		/**
		 * Folder Selection
		 */
		JPanel folderPanel = new JPanel();
		folderPanel.setBorder( new TitledBorder("Added Folders") );
		
		folderList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION);
		folderPanel.add( new JScrollPane(folderList) );
		JPanel folderButtons = new JPanel();
		folderButtons.add( jbtClearFolders );
		folderButtons.add( jbtRemoveFolder);
		folderPanel.add( folderButtons );
		
		add(folderPanel);
		
		/**
		 * Book Selection
		 */
		JPanel bookPanel = new JPanel();
		bookList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION);
		bookPanel.setBorder( new TitledBorder("Books") );
		bookPanel.add( new JScrollPane(bookList) );
		JPanel bookButtons = new JPanel();
		bookButtons.add( jbtOpen);
		bookPanel.add( bookButtons );
		add(bookPanel);
		
		/**
		 * Favorites Selection
		 */
		JPanel favoritesPanel = new JPanel();
		favoritesPanel.setBorder( new TitledBorder("Favorites") );
		JPanel favoritesButtons = new JPanel();
		favoritesButtons.setLayout( new GridLayout(0,1) );
		
		jbtRemoveFavorite.setEnabled( false );			/** Remove when added */
		jbtClearFavorites.setEnabled( false );			/** Remove when added */
		jbtOpenFavorite.setEnabled( false );			/** Remove when added */
			
		favoritesPanel.setLayout( new GridLayout(1,2));
		favoritesPanel.add( favoritesTree );
		favoritesButtons.add(jbtAddFavoriteBook);
		favoritesButtons.add(jbtAddFavoriteFolder);
		favoritesButtons.add(jbtRemoveFavorite);
		favoritesButtons.add(jbtClearFavorites);
		favoritesButtons.add( jbtOpenFavorite );
		favoritesPanel.add( favoritesButtons );
		add(favoritesPanel);
		
		/**
		 * Attributes Selection
		 */
		add(attributesPanel);
		
		
		
		//Listener to update the book list when the foldList is changed
		folderList.addListSelectionListener( new ListSelectionListener() {
				public void valueChanged(ListSelectionEvent e) {
					updateBookList();
				}
		});
		
		bookList.addListSelectionListener( new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				// update the Attributes Panel
				EBook book = (EBook)bookList.getSelectedValue();
				if (book != null) {
					EBookAttributes att = book.getAttributes();
				attributesPanel.getCurrentPageTextField().setText(""+att.currentPage);
				}
			}
			
		});
		
		//Setup the JFrame
		super.setSize(800,600);
		super.setLocationRelativeTo( null );
		super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		super.setVisible(true);
	}
	
	private void updateFolderList() {
		folderListModel.clear();
		for (int i =0; i < model.getNumberOfFolders(); i++)
			folderListModel.addElement(model.getFolder(i));
	}
	
	private void updateBookList() {
		bookListModel.clear();
		Folder folder = (Folder)folderList.getSelectedValue();
		if (folder != null) {
			for (int i =0; i < folder.getNumberOfFolders(); i++)  { //add folders in the folder
			bookListModel.addElement(folder.getFolder(i)); 
			}
			
			for (int i =0; i < folder.getNumberOfBooks(); i++)  { //add books in the folder
				bookListModel.addElement(folder.getBook(i)); 
			}	
		}
		else
			bookListModel.clear();
	}
	
	public void actionPerformed(ActionEvent e) {
		repaint();
		
		updateFolderList();	
	}
	
	public void setModel(OrganizerModel m) {
		model = m;
		
		if (model != null) {
			model.addActionListener(this);
		}
		
		updateFolderList();
		
	}
	
	public OrganizerModel getModel() {
		return model;
	}
	
	public JButton getOpenButton() {
		return this.jbtOpen;
	}
	
	public JMenuItem getReloadMenuItem() {
		return jmiReLoad; 
	}
	
	public JMenuItem getLoadMenuItem() {
		return jmiLoad;
	}
	
	public JMenuItem getSaveMenuItem() {
		return jmiSave;
	}
	
	public JMenuItem getSaveAsMenuItem() {
		return jmiSaveAs;
	}
	
	public JMenuItem getCloseMenuItem() {
		return jmiClose;
	}
	
	public JMenuItem getAddFolderMenuItem() {
		return jmiAddFolder;
	}
	
	public JMenuItem getSettingsMenuItem() {
		return jmiSettings;
	}
	
	public JMenuItem getAboutMenuItem() {
		return jmiAbout;
	}
	
	public JButton getRemoveFolderButton() {
		return jbtRemoveFolder;
	}
	
	public JButton getClearFoldersButton() {
		return jbtClearFolders;
	}
	
	public JList getFolderList() {
		return folderList;
	}
	
	public JList getBookList() {
		return bookList;
	}
	
	public AttributesPanel getAttributesPanel() {
		return attributesPanel;
	}

	public JButton getAddFavoriteBookButton() {
		return jbtAddFavoriteBook;
	}
	
	public JButton getAddFavoriteFolderButton() {
		return jbtAddFavoriteFolder;
	}
	
	public FavoritesTreePanel getFavoritesTree() {
		return favoritesTree;
	}
}
